package com.graduation.cdm.funactivity.view.mineview;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.graduation.cdm.funactivity.R;
import com.graduation.cdm.funactivity.bean.ActivityBean;
import com.graduation.cdm.funactivity.sqlitedraft.ActSqliteHelper;
import com.graduation.cdm.funactivity.util.CommonAdapter;
import com.graduation.cdm.funactivity.util.ViewHolder;
import com.graduation.cdm.funactivity.view.PublishActivity;
import com.mobeta.android.dslv.DragSortListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import static com.graduation.cdm.funactivity.util.DateUtils.stringToLong;

public class SqliteActivity extends AppCompatActivity {

    List<ActivityBean.Activity> activities = new ArrayList<>();
    ActivityBean.Activity activity;

    CommonAdapter<ActivityBean.Activity> activityCommonAdapter;
    private DragSortListView lvDraft;
    private ImageView back;

    public static final int CAO_GAO = 1;
    Long beg ;
    Long end ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);

        initView();
        initEvent();

    }

    private void initView(){
        lvDraft = ((DragSortListView) findViewById(R.id.lv_caogao));
        back = ((ImageView) findViewById(R.id.back));

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        lvDraft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(), PublishActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("activityInfo",activities.get(position));
                intent.putExtras(bundle);
                startActivityForResult(intent, CAO_GAO);
                finish();
            }
        });

        lvDraft.setRemoveListener(new DragSortListView.RemoveListener() {
            @Override
            public void remove(int i) {
                ActivityBean.Activity activity = activities.get(i);
                activityCommonAdapter.remove(i);
                ActSqliteHelper dbHelper = new ActSqliteHelper(SqliteActivity.this);
                //得到一个可写的数据库
                SQLiteDatabase db = dbHelper.getReadableDatabase();
                String whereClauses = "activity_creattime=?";
                String[] whereArgs = new String[]{String.valueOf(activity.activityCreattime)};
                //调用delete方法，删除数据
                db.delete("act_table", whereClauses, whereArgs);
            }
        });

    }

    public void initEvent() {
        querydb();

        activityCommonAdapter = new CommonAdapter<ActivityBean.Activity>(getApplicationContext(), activities, R.layout.activity_sqlite_item) {
            @Override
            public void convert(ViewHolder viewHolder, ActivityBean.Activity activity, int position) {
                TextView title = viewHolder.getViewById(R.id.title);
                TextView time = viewHolder.getViewById(R.id.time);

                title.setText(activity.activityTheme);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String activity_creattime = sdf.format(activity.activityCreattime);
                time.setText(activity_creattime);
            }
        };

        lvDraft.setAdapter(activityCommonAdapter);

    }

    public void querydb() {
        ActSqliteHelper dbHelper = new ActSqliteHelper(SqliteActivity.this);
        //得到一个可写的数据库
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        //参数1：表名
        //参数2：要想显示的列
        //参数3：where子句
        //参数4：where子句对应的条件值
        //参数5：分组方式
        //参数6：having条件
        //参数7：排序方式
        Cursor cursor = db.query("act_table", new String[]{"activity_id", "user_id", "activity_label", "activity_theme", "activity_creattime", "activity_begintime", "activity_imgurl", "activity_desc",
                        "activity_care", "activity_province", "activity_endtime", "activity_address", "activity_max_people_number", "activity_city"},"user_id=?", new String[]{"4"}, null, null, null);
        while (cursor.moveToNext()) {
            String userId = cursor.getString(cursor.getColumnIndex("user_id"));
            String activity_id = cursor.getString(cursor.getColumnIndex("activity_id"));
            String activity_label = cursor.getString(cursor.getColumnIndex("activity_label"));
            String activity_theme = cursor.getString(cursor.getColumnIndex("activity_theme"));
            String activity_begintime = cursor.getString(cursor.getColumnIndex("activity_begintime"));
            String activity_creattime = cursor.getString(cursor.getColumnIndex("activity_creattime"));
            String activity_imgurl = cursor.getString(cursor.getColumnIndex("activity_imgurl"));
            String activity_desc = cursor.getString(cursor.getColumnIndex("activity_desc"));
            String activity_care = cursor.getString(cursor.getColumnIndex("activity_care"));
            String province = cursor.getString(cursor.getColumnIndex("activity_province"));
            String activity_endtime = cursor.getString(cursor.getColumnIndex("activity_endtime"));
            String activity_address = cursor.getString(cursor.getColumnIndex("activity_address"));
            String activity_max_people_number = cursor.getString(cursor.getColumnIndex("activity_max_people_number"));
            String city = cursor.getString(cursor.getColumnIndex("activity_city"));

            if (activity_begintime.equals("")) {
                activity_begintime = "2000-01-01 00:00:00";
                beg = stringToLong(activity_begintime);
            } else {
                beg = stringToLong(activity_begintime + ":00");
            }
            if (activity_endtime.equals("")) {
                activity_endtime = "2000-01-01 00:00:00";
                end = stringToLong(activity_endtime);
            } else {
                end = stringToLong(activity_endtime + ":00");
            }

            ActivityBean activityBean = new ActivityBean();
            activity = activityBean.new Activity(activity_label,Integer.parseInt(userId),activity_theme,beg,activity_imgurl,activity_desc,activity_care,end,
                    activity_address,Integer.parseInt(activity_max_people_number),1,Long.parseLong(activity_creattime),0,province,city);


            activities.add(activity);
        }

        //关闭数据库
        db.close();
        System.out.println("activities=============" + activities);
    }

}
